package com.xbongbong.paas.help.optional.range.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.help.optional.range.UserDeptOptionalRangeHandler;
import com.xbongbong.paas.pojo.dto.UserInfoDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.ProductUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @Description 成员角色条件过滤
 * @date 2021/9/9 10:12
 * @Version 1.0
 */
public class RoleNameHandlerImpl implements UserDeptOptionalRangeHandler {
    private static final Logger LOG = LoggerFactory.getLogger(RoleNameHandlerImpl.class);

    @Override
    public Map<String, Object> filterDeptUserData(FilterConditionsPoJo filterConditionsPoJo, JSONObject optionalRangeDataList, PaasFormDataEntity paasFormDataEntity, Map<Integer, List<UserTeamEntity>> userTeam, List<ProductUserEntity> productUserList, UserInfoDTO loginUserInfo) throws XbbException {
        List<Integer> roleIdIn = null;
        List<Integer> roleIdNoIn = null;
        boolean conditionSymbol = true;
        HashMap<String, Object> userMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        try {
            JSONArray roles = JSON.parseArray(JSON.toJSONString(filterConditionsPoJo.getValue()));
            if (Objects.equals(filterConditionsPoJo.getSymbol(), ConditionEnum.NOIN.getSymbol())){
                conditionSymbol = false;
                roleIdNoIn = new ArrayList<>();
            }else {
                roleIdIn = new ArrayList<>();
            }
            if (Objects.nonNull(roles)){
                for (int i = 0; i < roles.size(); i++) {
                    JSONObject role = roles.getJSONObject(i);
                    if (Objects.nonNull(role)){
                        Integer roleId = Integer.valueOf(role.getInteger("value"));
                        if (conditionSymbol) {
                            roleIdIn.add(roleId);
                        } else {
                            roleIdNoIn.add(roleId);
                        }
                    }
                }

            }
            if (Objects.nonNull(roleIdIn)){
                userMap.put("roleIdIn", roleIdIn);
            }
            if (Objects.nonNull(roleIdNoIn)){
                userMap.put("roleIdNoIn", roleIdNoIn);
            }
        } catch (Exception e) {
            LOG.error("RoleNameHandlerImpl.filterDeptUserData 执行错误", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return userMap;
    }

}

